package org.geektimes.projects.user.web.controller;

import org.geektimes.projects.user.web.model.GiteeConstants;
import org.geektimes.web.mvc.controller.PageController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @Author: jipeng
 * @Description:
 * @Date: Created in 2021/4/22 7:49
 */
@Path("/login")
public class LoginController implements PageController {


    String oauthPath = "https://gitee.com/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code";


    @GET
    @POST
    @Path("/index") // /hello/world -> HelloWorldController
    public String index(HttpServletRequest request, HttpServletResponse response) throws Throwable {

        String callbackUrl = URLEncoder.encode(GiteeConstants.callbackUrl, "UTF-8");
        String path = String.format(oauthPath, GiteeConstants.clientId, callbackUrl);

        request.setAttribute("giteeUrl", path);
        return "login-form.jsp";
    }
}
